package com.rt.web.controller.system;

import java.util.List;

import com.rt.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rt.common.annotation.Log;
import com.rt.common.core.controller.BaseController;
import com.rt.common.core.domain.AjaxResult;
import com.rt.common.enums.BusinessType;
import com.rt.system.domain.RtQuestionTypeModule;
import com.rt.system.service.IRtQuestionTypeModuleService;
import com.rt.common.utils.poi.ExcelUtil;
import com.rt.common.core.page.TableDataInfo;

/**
 * 题型模块Controller
 * 
 * @author Yg
 * @date 2022-04-13
 */
@RestController
@RequestMapping("/system/module")
public class RtQuestionTypeModuleController extends BaseController
{
    @Autowired
    private IRtQuestionTypeModuleService rtQuestionTypeModuleService;

    /**
     * 查询题型模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(RtQuestionTypeModule rtQuestionTypeModule)
    {
        startPage();
        List<RtQuestionTypeModule> list = rtQuestionTypeModuleService.selectRtQuestionTypeModuleList(rtQuestionTypeModule);
        return getDataTable(list);
    }

    /**
     * 查询题型模块列表
     */
    @GetMapping("/availableList")
    public TableDataInfo availableList(RtQuestionTypeModule rtQuestionTypeModule)
    {
        rtQuestionTypeModule.setStatus(1);
        List<RtQuestionTypeModule> list = rtQuestionTypeModuleService.selectRtQuestionTypeModuleList(rtQuestionTypeModule);
        return getDataTable(list);
    }

    /**
     * 导出题型模块列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:export')")
    @Log(title = "题型模块", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RtQuestionTypeModule rtQuestionTypeModule)
    {
        List<RtQuestionTypeModule> list = rtQuestionTypeModuleService.selectRtQuestionTypeModuleList(rtQuestionTypeModule);
        ExcelUtil<RtQuestionTypeModule> util = new ExcelUtil<RtQuestionTypeModule>(RtQuestionTypeModule.class);
        return util.exportExcel(list, "题型模块数据");
    }

    /**
     * 获取题型模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(rtQuestionTypeModuleService.selectRtQuestionTypeModuleById(id));
    }

    /**
     * 新增题型模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:add')")
    @Log(title = "题型模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RtQuestionTypeModule rtQuestionTypeModule)
    {
        if (StringUtils.isNotNull(rtQuestionTypeModuleService.selectRtQuestionTypeModuleByName(rtQuestionTypeModule))){
            return AjaxResult.error("题型模块名称已存在");
        }
        rtQuestionTypeModule.setCreateOperator(getUsername());
        return toAjax(rtQuestionTypeModuleService.insertRtQuestionTypeModule(rtQuestionTypeModule));
    }

    /**
     * 修改题型模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:edit')")
    @Log(title = "题型模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RtQuestionTypeModule rtQuestionTypeModule)
    {
        if (StringUtils.isNotNull(rtQuestionTypeModuleService.selectRtQuestionTypeModuleByName(rtQuestionTypeModule))){
            return AjaxResult.error("题型模块名称已存在");
        }
        rtQuestionTypeModule.setUpdateOperator(getUsername());
        return toAjax(rtQuestionTypeModuleService.updateRtQuestionTypeModule(rtQuestionTypeModule));
    }

    /**
     * 删除题型模块
     */
    @PreAuthorize("@ss.hasPermi('system:module:remove')")
    @Log(title = "题型模块", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rtQuestionTypeModuleService.deleteRtQuestionTypeModuleByIds(ids));
    }
}
